﻿Imports System.Globalization
Partial Public Class Slop
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "SLOP "
            lbDateSHow.Text = "DATE OF " & Date.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")) & " (Midnight - CYCLE) "
            ViewState("Date_") = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
            LoadSlop(Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US")))
        End If
    End Sub

    Private Sub LoadSlop(ByVal _D As String)
        ViewState("Slop") = clsGet.Get_Slop("", "")
        gridItem.PageIndex = 0
        gridItem.DataSource = ViewState("Slop")
        gridItem.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("Slop")
        gridItem.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("Slop")
        gridItem.DataBind()
    End Sub


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub gridItem_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "SLOP TANK NO."
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "OPENNING STOCK (Litres)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "RECEIVED FROM (Litres)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "TRANSFER TO TF2 (Litres)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PHYSICAL BALANCE (Litres)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REMARK"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow1)

            table = New TableCell()
            table.Text = "LINE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "HOSE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow)
        End If
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtPhysical As TextBox = CType(e.Row.FindControl("txtPhysical"), TextBox)
            Dim txtRemark As TextBox = CType(e.Row.FindControl("txtRemark"), TextBox)
            Dim Date_ As String = ViewState("Date_")
            Dim Chk As Boolean = True
            Dim dtslop As DataTable
            For i As Integer = 1 To 30
                Dim D As String = CDate(Date_).AddYears(543).AddDays(-i).ToString("dd MMM yyyy", New CultureInfo("en-US"))
                dtslop = clsGet.Get_SlopForDate(D, gridItem.DataKeys(e.Row.RowIndex).Values("Slop_Code"))
                If (dtslop.Rows.Count > 0) Then
                    Chk = False
                    e.Row.Cells(1).Text = dtslop.Rows(0).Item("Slop_Value")
                    Exit For
                End If
            Next
            If (Chk) Then
                e.Row.Cells(1).Text = "0"
            End If
            dtslop = clsGet.Get_SlopForDate(Date_, gridItem.DataKeys(e.Row.RowIndex).Values("Slop_Code"))
            If (dtslop.Rows.Count > 0) Then
                Chk = False
                txtPhysical.Text = FormatNumber(dtslop.Rows(0).Item("Slop_Value"))
                txtRemark.Text = dtslop.Rows(0).Item("Slop_Remark")
                Dim radioAction As RadioButtonList = CType(e.Row.FindControl("radioAction"), RadioButtonList)
                If (dtslop.Rows(0).Item("Slop_Action") = "H") Then
                    e.Row.Cells(2).Text = "0.00"
                    e.Row.Cells(3).Text = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(e.Row.Cells(1).Text))
                    e.Row.Cells(4).Text = "0.00"
                    radioAction.SelectedValue = "H"
                ElseIf (dtslop.Rows(0).Item("Slop_Action") = "L") Then
                    e.Row.Cells(2).Text = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(e.Row.Cells(1).Text))
                    e.Row.Cells(3).Text = "0.00"
                    e.Row.Cells(4).Text = "0.00"
                    radioAction.SelectedValue = "L"
                Else
                    e.Row.Cells(2).Text = "0.00"
                    e.Row.Cells(3).Text = "0.00"
                    e.Row.Cells(4).Text = FormatNumber(CDbl(e.Row.Cells(1).Text) - CDbl(dtslop.Rows(0).Item("Slop_Value")))
                End If
            Else
                txtPhysical.Text = "0.00"
                txtRemark.Text = String.Empty
                e.Row.Cells(2).Text = "0.00"
                e.Row.Cells(3).Text = "0.00"
                e.Row.Cells(4).Text = "0.00"
            End If
            Chk = False
            If (Chk) Then
                txtPhysical.Text = "0.00"
                txtRemark.Text = String.Empty
                e.Row.Cells(1).Text = "0.00"
                e.Row.Cells(2).Text = "0.00"
                e.Row.Cells(3).Text = "0.00"
                e.Row.Cells(4).Text = "0.00"
            End If
        End If
    End Sub



    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If (txtDate.Text.Trim = "") Then
            lbDateSHow.Text = "DATE OF " & Date.Now.ToString("dd MMMM yyyy", New CultureInfo("en-US")) & " (Midnight - CYCLE) "
            ViewState("Date_") = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
            LoadSlop(Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US")))
        Else
            ViewState("Date_") = txtDate.Text
            lbDateSHow.Text = "DATE OF " & CDate(txtDate.Text).ToString("dd MMMM ", New CultureInfo("en-US")) & txtDate.Text.Split(" ")(2) & " (Midnight - CYCLE) "
            LoadSlop(txtDate.Text)
        End If

    End Sub

    Private Sub gridItem_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridItem.RowEditing

    End Sub

    Private Function ChkValue() As Boolean
        For i As Integer = 0 To gridItem.Rows.Count - 1
            Dim txtPhysical As TextBox = CType(gridItem.Rows(i).FindControl("txtPhysical"), TextBox)
            Dim radioAction As RadioButtonList = CType(gridItem.Rows(i).FindControl("radioAction"), RadioButtonList)
            If (txtPhysical.Text.Trim = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุปริมาณ Physical Balance ของ Slop " & gridItem.Rows(i).Cells(0).Text & " !!');", True)
                Return False
            End If
            If (CDbl(txtPhysical.Text) > CDbl(gridItem.Rows(i).Cells(1).Text)) Then
                If Not ((radioAction.SelectedValue = "L") Or (radioAction.SelectedValue = "H")) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือก Line หรือ Hose ใน Slop " & gridItem.Rows(i).Cells(0).Text & " !!');", True)
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (ChkValue()) Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            Dim dtslop As DataTable = clsGet.Get_SlopForDate(ViewState("Date_"), "")
            tran = clsDB.BeginTran(conn)
            For i As Integer = 0 To gridItem.Rows.Count - 1
                Dim txtPhysical As TextBox = CType(gridItem.Rows(i).FindControl("txtPhysical"), TextBox)
                Dim txtRemark As TextBox = CType(gridItem.Rows(i).FindControl("txtRemark"), TextBox)
                Dim radioAction As RadioButtonList = CType(gridItem.Rows(i).FindControl("radioAction"), RadioButtonList)
                Dim Action As String = ""
                If (CDbl(txtPhysical.Text) > CDbl(gridItem.Rows(i).Cells(1).Text)) Then
                    Action = radioAction.SelectedValue
                Else
                    Action = "T"
                End If
                Dim dtrow As DataRow() = dtslop.Select("Slop_Code = '" & gridItem.DataKeys(i).Values("Slop_Code") & "'")
                Dim Plus As Double = 0.0
                If (dtrow.Count > 0) Then
                    If (CDbl(dtrow(0).Item("Slop_Value")) > CDbl(txtPhysical.Text)) Then
                        Plus = CDbl(dtrow(0).Item("Slop_Value")) - CDbl(txtPhysical.Text)
                    Else
                        Plus = CDbl(txtPhysical.Text) - CDbl(dtrow(0).Item("Slop_Value"))
                    End If
                Else
                    Plus = txtPhysical.Text
                End If
                clsFc.DeleteSlop_Transection(gridItem.DataKeys(i).Values("Slop_Code"), ViewState("Date_"), conn, tran)
                If (clsFc.InsertSlop_Transection(gridItem.DataKeys(i).Values("Slop_Code"), ViewState("Date_"), txtPhysical.Text.Replace(",", ""), Action, CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), Date.Now.ToString("dd MMM yyyy HH:mm:ss"), txtRemark.Text, conn, tran) > 0) Then
                    clsFc.Update_Slop(gridItem.DataKeys(i).Values("Slop_Code"), Plus, ViewState("Date_").ToString.Split(" ")(2) & CDate(ViewState("Date_")).AddDays(1).ToString("MMdd"), conn, tran)
                Else
                condition = False
                End If
            Next

            If (condition) Then
                clsDB.CommitTran(tran)
                LoadSlop(ViewState("Date_"))
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลเรียบร้อย');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก');", True)
            End If

            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dt As DataTable = Create()
        Dim Excise As Double = 0
        dt = LoadValue(dt, Excise)
        dsReport.Tables.Add(dt.Copy)
        dsReport.AcceptChanges()
        ReportName = "Slop_Report.rdlc"
        reportDataSource = "slop_Value"

        paraName = "DateSlop;Excise"
        paraValue = ViewState("Date_") & ";" & Excise
        Orientation = False
        reportType = "pdf"

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function LoadValue(ByVal dt As DataTable, ByRef Excise As Double) As DataTable
        Dim slop As DataTable = clsGet.Get_Slop("", "")
        Dim Color As DataTable = clsGet.Get_StockMaster("", "", "", "'C','A'")
        Dim Marker As DataTable = clsGet.Get_StockMaster("", "", "", "'M'")
        Dim L As Integer = 0
        If (L < slop.Rows.Count - 1) Then
            L = slop.Rows.Count - 1
        End If
        If (L < Color.Rows.Count - 1) Then
            L = Color.Rows.Count - 1
        End If
        If (L < Marker.Rows.Count - 1) Then
            L = Marker.Rows.Count - 1
        End If
        For x As Integer = 0 To L
            'Slop
            Dim dr As DataRow = dt.NewRow()
            Dim Date_ As String = ViewState("Date_")
            Dim _D As String = Date_.Split(" ")(2) & CDate(Date_).ToString("MMdd")
            If (x > slop.Rows.Count - 1) Then
                dr.Item("SLOP") = slop.Rows(slop.Rows.Count - 1).Item("Slop_Name")
                Dim Chk As Boolean = True
                Dim dtslop As DataTable
                For i As Integer = 1 To 30
                    Dim D As String = CDate(Date_).AddYears(543).AddDays(-i).ToString("dd MMM yyyy", New CultureInfo("en-US"))
                    dtslop = clsGet.Get_SlopForDate(D, slop.Rows(slop.Rows.Count - 1).Item("Slop_Code"))
                    If (dtslop.Rows.Count > 0) Then
                        Chk = False
                        dr.Item("S_OPEN") = dtslop.Rows(0).Item("Slop_Value")
                        Exit For
                    End If
                Next
                If (Chk) Then
                    dr.Item("S_OPEN") = "0"
                End If

                dtslop = clsGet.Get_SlopForDate(Date_, slop.Rows(slop.Rows.Count - 1).Item("Slop_Code"))
                If (dtslop.Rows.Count > 0) Then
                    Chk = False
                    dr.Item("S_BALANCE") = FormatNumber(dtslop.Rows(0).Item("Slop_Value"))
                    dr.Item("S_REMARK") = dtslop.Rows(0).Item("Slop_Remark") & ""
                    If (dtslop.Rows(0).Item("Slop_Action") = "H") Then
                        dr.Item("S_LINE") = "0.00"
                        dr.Item("S_HOSE") = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(dr.Item("S_OPEN")))
                        dr.Item("S_TRAN") = "0.00"
                    ElseIf (dtslop.Rows(0).Item("Slop_Action") = "L") Then
                        dr.Item("S_LINE") = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(dr.Item("S_OPEN")))
                        dr.Item("S_HOSE") = "0.00"
                        dr.Item("S_TRAN") = "0.00"
                    Else
                        dr.Item("S_LINE") = "0.00"
                        dr.Item("S_HOSE") = "0.00"
                        dr.Item("S_TRAN") = FormatNumber(CDbl(dr.Item("S_OPEN")) - CDbl(dtslop.Rows(0).Item("Slop_Value")))
                    End If
                Else
                    dr.Item("S_BALANCE") = "0.00"
                    dr.Item("S_REMARK") = String.Empty
                    dr.Item("S_LINE") = "0.00"
                    dr.Item("S_HOSE") = "0.00"
                    dr.Item("S_TRAN") = "0.00"
                End If
            Else
                dr.Item("SLOP") = slop.Rows(x).Item("Slop_Name")
                Dim Chk As Boolean = True
                Dim dtslop As DataTable
                For i As Integer = 1 To 30
                    Dim D As String = CDate(Date_).AddYears(543).AddDays(-i).ToString("dd MMM yyyy", New CultureInfo("en-US"))
                    dtslop = clsGet.Get_SlopForDate(D, slop.Rows(x).Item("Slop_Code"))
                    If (dtslop.Rows.Count > 0) Then
                        Chk = False
                        dr.Item("S_OPEN") = dtslop.Rows(0).Item("Slop_Value")
                        Exit For
                    End If
                Next
                If (Chk) Then
                    dr.Item("S_OPEN") = "0"
                End If

                dtslop = clsGet.Get_SlopForDate(Date_, slop.Rows(x).Item("Slop_Code"))
                If (dtslop.Rows.Count > 0) Then
                    Chk = False
                    dr.Item("S_BALANCE") = FormatNumber(dtslop.Rows(0).Item("Slop_Value"))
                    dr.Item("S_REMARK") = dtslop.Rows(0).Item("Slop_Remark") & ""
                    If (dtslop.Rows(0).Item("Slop_Action") = "H") Then
                        dr.Item("S_LINE") = "0.00"
                        dr.Item("S_HOSE") = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(dr.Item("S_OPEN")))
                        dr.Item("S_TRAN") = "0.00"
                    ElseIf (dtslop.Rows(0).Item("Slop_Action") = "L") Then
                        dr.Item("S_LINE") = FormatNumber(CDbl(dtslop.Rows(0).Item("Slop_Value")) - CDbl(dr.Item("S_OPEN")))
                        dr.Item("S_HOSE") = "0.00"
                        dr.Item("S_TRAN") = "0.00"
                    Else
                        dr.Item("S_LINE") = "0.00"
                        dr.Item("S_HOSE") = "0.00"
                        dr.Item("S_TRAN") = FormatNumber(CDbl(dr.Item("S_OPEN")) - CDbl(dtslop.Rows(0).Item("Slop_Value")))
                    End If
                Else
                    dr.Item("S_BALANCE") = "0.00"
                    dr.Item("S_REMARK") = String.Empty
                    dr.Item("S_LINE") = "0.00"
                    dr.Item("S_HOSE") = "0.00"
                    dr.Item("S_TRAN") = "0.00"
                End If
            End If
            'Color
            If (x > Color.Rows.Count - 1) Then
                dr.Item("Color") = Color.Rows(Color.Rows.Count - 1).Item("Grade")
                Dim Amount As Double = CDbl(Color.Rows(Color.Rows.Count - 1).Item("Amount"))
                Dim Recieve As Double = 0
                Dim Transfer As Double = 0
                Dim remark As String = ""
                Dim dtAction As DataTable = clsGet.Get_StockActionToCurrent(Color.Rows(Color.Rows.Count - 1).Item("Stock_Code"), _D, "99999999")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Amount -= CDbl(da.Item("Amount"))
                        Else
                            Amount += CDbl(da.Item("Amount"))
                        End If
                    Next
                End If
                'Daily
                dtAction = clsGet.Get_StockActionToCurrent(Color.Rows(Color.Rows.Count - 1).Item("Stock_Code"), _D, _D)
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(da.Item("Booking_no")).Tables(0)
                            If (dsBooking.Rows.Count > 0) Then
                                If (remark.Trim() = "") Then
                                    remark = dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                Else
                                    remark = "/" & dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                End If
                            End If
                        Else
                            If (da.Item("Remark") & "" <> "") Then
                                If (remark = "") Then
                                    remark = da.Item("Remark") & ""
                                Else
                                    remark = "/" & da.Item("Remark") & ""
                                End If
                            End If
                        End If

                    Next
                End If
                dr.Item("C_RD") = Recieve
                dr.Item("C_TD") = Transfer
                dr.Item("C_BALANCE") = Amount + Recieve - Transfer
                dr.Item("C_OPEN") = Amount

                dr.Item("C_SHIP") = remark
                'Monthly
                Recieve = 0
                Transfer = 0
                dtAction = clsGet.Get_StockActionToCurrent(Color.Rows(Color.Rows.Count - 1).Item("Stock_Code"), _D, _D.Substring(0, 6) & "32")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If

                    Next
                End If
                dr.Item("C_RM") = Recieve
                dr.Item("C_TM") = Transfer
            Else
                dr.Item("Color") = Color.Rows(x).Item("Grade")
                Dim Amount As Double = CDbl(Color.Rows(x).Item("Amount"))
                Dim Recieve As Double = 0
                Dim Transfer As Double = 0
                Dim remark As String = ""
                Dim dtAction As DataTable = clsGet.Get_StockActionToCurrent(Color.Rows(x).Item("Stock_Code"), _D, "99999999")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Amount -= CDbl(da.Item("Amount"))
                        Else
                            Amount += CDbl(da.Item("Amount"))
                        End If
                    Next
                End If
                'Daily
                dtAction = clsGet.Get_StockActionToCurrent(Color.Rows(x).Item("Stock_Code"), _D, _D)
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(da.Item("Booking_no")).Tables(0)
                            If (dsBooking.Rows.Count > 0) Then
                                If (remark.Trim() = "") Then
                                    remark = dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                Else
                                    remark = "/" & dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                End If
                            End If
                        Else
                            If (da.Item("Remark") & "" <> "") Then
                                If (remark = "") Then
                                    remark = da.Item("Remark") & ""
                                Else
                                    remark = "/" & da.Item("Remark") & ""
                                End If
                            End If
                        End If

                    Next
                End If
                dr.Item("C_RD") = Recieve
                dr.Item("C_TD") = Transfer
                dr.Item("C_BALANCE") = Amount + Recieve - Transfer
                dr.Item("C_OPEN") = Amount

                dr.Item("C_SHIP") = remark
                'Monthly
                Recieve = 0
                Transfer = 0
                dtAction = clsGet.Get_StockActionToCurrent(Color.Rows(x).Item("Stock_Code"), _D, _D.Substring(0, 6) & "32")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If

                    Next
                End If
                dr.Item("C_RM") = Recieve
                dr.Item("C_TM") = Transfer
            End If

            'Marker
            If (x > Marker.Rows.Count - 1) Then
                dr.Item("Marker") = Marker.Rows(Marker.Rows.Count - 1).Item("Grade")
                Dim Amount As Double = CDbl(Marker.Rows(Marker.Rows.Count - 1).Item("Amount"))
                Dim Recieve As Double = 0
                Dim Transfer As Double = 0
                Dim remark As String = ""
                Dim dtAction As DataTable = clsGet.Get_StockActionToCurrent(Marker.Rows(Marker.Rows.Count - 1).Item("Stock_Code"), _D, "99999999")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Amount -= CDbl(da.Item("Amount"))
                        Else
                            Amount += CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                            If (dtEx.Rows.Count > 0) Then
                                If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                    Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Stock_Code"))
                                    If (Relation.Rows.Count > 0) Then
                                        Try
                                            Amount += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                        Catch ex As Exception

                                        End Try

                                    End If
                                End If
                            End If
                        End If
                    Next
                End If
                'Daily
                dtAction = clsGet.Get_StockActionToCurrent(Marker.Rows(Marker.Rows.Count - 1).Item("Stock_Code"), _D, _D)
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                            If (dtEx.Rows.Count > 0) Then
                                If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                    Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Booking_no"))
                                    If (Relation.Rows.Count > 0) Then
                                        Try
                                            Excise += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                        Catch ex As Exception

                                        End Try

                                    End If
                                End If
                            End If

                            Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(da.Item("Booking_no")).Tables(0)
                            If (dsBooking.Rows.Count > 0) Then
                                If (remark.Trim() = "") Then
                                    remark = dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                Else
                                    remark = "/" & dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                End If
                            End If
                        Else
                            If (da.Item("Remark") & "" <> "") Then
                                If (remark = "") Then
                                    remark = da.Item("Remark") & ""
                                Else
                                    remark = "/" & da.Item("Remark") & ""
                                End If
                            End If
                        End If

                    Next
                End If
                dr.Item("M_RD") = Recieve
                dr.Item("M_TD") = Transfer
                dr.Item("M_BALANCE") = Amount + Recieve - Transfer
                dr.Item("M_OPEN") = Amount
                dr.Item("M_SHIP") = remark
                'Monthly
                Recieve = 0
                Transfer = 0
                dtAction = clsGet.Get_StockActionToCurrent(Marker.Rows(Marker.Rows.Count - 1).Item("Stock_Code"), _D, _D.Substring(0, 6) & "32")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If

                    Next
                End If
                dr.Item("M_RM") = Recieve
                dr.Item("M_TM") = Transfer
            Else
                dr.Item("Marker") = Marker.Rows(x).Item("Grade")
                Dim Amount As Double = CDbl(Marker.Rows(x).Item("Amount"))
                Dim Recieve As Double = 0
                Dim Transfer As Double = 0
                Dim remark As String = ""
                Dim dtAction As DataTable = clsGet.Get_StockActionToCurrent(Marker.Rows(x).Item("Stock_Code"), _D, "99999999")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Amount += CDbl(da.Item("Amount"))
                        Else
                            Amount -= CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                            If (dtEx.Rows.Count > 0) Then
                                If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                    Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Booking_no"))
                                    If (Relation.Rows.Count > 0) Then
                                        Try
                                            Amount += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                        Catch ex As Exception

                                        End Try

                                    End If
                                End If
                            End If
                        End If
                    Next
                End If
                'Daily
                dtAction = clsGet.Get_StockActionToCurrent(Marker.Rows(x).Item("Stock_Code"), _D, _D)
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If
                        If (da.Item("Type_Event") & "" = "C") Then
                            Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                            If (dtEx.Rows.Count > 0) Then
                                If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                    Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Booking_no"))
                                    If (Relation.Rows.Count > 0) Then
                                        Try
                                            Excise += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                        Catch ex As Exception

                                        End Try

                                    End If
                                End If
                            End If

                            Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(da.Item("Booking_no")).Tables(0)
                            If (dsBooking.Rows.Count > 0) Then
                                If (remark.Trim() = "") Then
                                    remark = dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                Else
                                    remark = "/" & dsBooking.Rows(0).Item("Vehicle_Text") & ""
                                End If
                            End If
                        Else
                            If (da.Item("Remark") & "" <> "") Then
                                If (remark = "") Then
                                    remark = da.Item("Remark") & ""
                                Else
                                    remark = "/" & da.Item("Remark") & ""
                                End If
                            End If
                        End If
                    Next
                End If
                dr.Item("M_RD") = Recieve
                dr.Item("M_TD") = Transfer
                dr.Item("M_BALANCE") = Amount + Recieve - Transfer
                dr.Item("M_OPEN") = Amount
                dr.Item("M_SHIP") = remark
                'Monthly
                Recieve = 0
                Transfer = 0
                dtAction = clsGet.Get_StockActionToCurrent(Marker.Rows(x).Item("Stock_Code"), _D, _D.Substring(0, 6) & "32")
                If (dtAction.Rows.Count > 0) Then
                    For Each da As DataRow In dtAction.Rows
                        If (da.Item("Type_Event") & "" = "A") Then
                            Recieve += CDbl(da.Item("Amount"))
                        Else
                            Transfer += CDbl(da.Item("Amount"))
                        End If

                    Next
                End If
                dr.Item("M_RM") = Recieve
                dr.Item("M_TM") = Transfer
            End If

            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next
        For Each dr As DataRow In dt.Rows
            dr.Item("M_BALANCE") = CDbl(dr.Item("M_BALANCE")) - Excise
            dt.AcceptChanges()
        Next


        Return dt
    End Function

    Private Function Create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("SLOP", GetType(String))
        dt.Columns.Add("S_OPEN", GetType(String))
        dt.Columns.Add("S_LINE", GetType(String))
        dt.Columns.Add("S_HOSE", GetType(String))
        dt.Columns.Add("S_TRAN", GetType(String))
        dt.Columns.Add("S_BALANCE", GetType(String))
        dt.Columns.Add("S_REMARK", GetType(String))
        dt.Columns.Add("Color", GetType(String))
        dt.Columns.Add("C_OPEN", GetType(String))
        dt.Columns.Add("C_RM", GetType(String))
        dt.Columns.Add("C_RD", GetType(String))
        dt.Columns.Add("C_TM", GetType(String))
        dt.Columns.Add("C_TD", GetType(String))
        dt.Columns.Add("C_BALANCE", GetType(String))
        dt.Columns.Add("C_SHIP", GetType(String))
        dt.Columns.Add("Marker", GetType(String))
        dt.Columns.Add("M_OPEN", GetType(String))
        dt.Columns.Add("M_RM", GetType(String))
        dt.Columns.Add("M_RD", GetType(String))
        dt.Columns.Add("M_TM", GetType(String))
        dt.Columns.Add("M_TD", GetType(String))
        dt.Columns.Add("M_BALANCE", GetType(String))
        dt.Columns.Add("M_SHIP", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function
End Class